using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;   
using System.IO;   
using System.Text;   

public class XMLSaveLoad
{
	public static void SerializeObject<T>(T obj, string path)   
	{  
		StreamWriter writer = new StreamWriter(path, false);  

		XmlSerializer xs = new XmlSerializer(typeof(T)); 
		xs.Serialize(writer, obj);    
 
		writer.Close();   
	}   

	public static T DeserializeObject<T>(string path)   
	{   
		StreamReader reader = new StreamReader(path);
		XmlSerializer xs = new XmlSerializer(typeof(T));   
		T obj = (T)xs.Deserialize(reader);   
		reader.Close();
		return obj;
	}   

//	public static void SerializeObject<T>(T obj, string path)   
//	{  
//		XmlTextWriter xmlTextWriter = new XmlTextWriter(new MemoryStream(), Encoding.UTF8);   
//		XmlSerializer xs = new XmlSerializer(typeof(T)); 
//		xs.Serialize(xmlTextWriter, obj);    
//		string xmlContent = ByteArrayToString(
//			((MemoryStream)xmlTextWriter.BaseStream).ToArray());   
//		
//		StreamWriter writer = new StreamWriter(path, false);  
//		writer.Write(xmlContent);   
//		writer.Close();   
//	}   
	
//	public static T DeserializeObject<T>(string path)   
//	{   
//		StreamReader reader = new StreamReader(path);
//		string xmlContent = reader.ReadToEnd();
//		reader.Close();
//		
//		XmlSerializer xs = new XmlSerializer(typeof(T));   
//		MemoryStream memoryStream = new MemoryStream(StringToByteArray(xmlContent));   
//		XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);   
//		return (T)xs.Deserialize(memoryStream);   
//	}   
	
	static string ByteArrayToString(byte[] characters)   
	{        
		UTF8Encoding encoding = new UTF8Encoding();   
		string constructedString = encoding.GetString(characters);   
		return (constructedString);   
	}   
	
	static byte[] StringToByteArray(string pXmlString)   
	{   
		UTF8Encoding encoding = new UTF8Encoding();  
		byte[] byteArray = encoding.GetBytes(pXmlString);   
		return byteArray;   
	}   
	

}

